/*
 * Copyright 2014 Cungmua, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cungmua.vn.kiosk.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import cungmua.vn.kiosk.AbstractActivity;
import cungmua.vn.kiosk.R;
import cungmua.vn.kiosk.activity.DangGiaoActivity.getDonHang;
import cungmua.vn.kiosk.adapter.DonHangAdapter;
import cungmua.vn.kiosk.adapter.DonHangTraVeAdapter;
import cungmua.vn.kiosk.compone.LoadMoreListView;
import cungmua.vn.kiosk.db.DatabaseHandler;
import cungmua.vn.kiosk.handle.DonHangHandler;
import cungmua.vn.kiosk.handle.ServiceHandler;
import cungmua.vn.kiosk.model.DonHang;
import cungmua.vn.kiosk.model.NhanVien;
import cungmua.vn.kiosk.network.CallServices;
import cungmua.vn.kiosk.tabs.TabGroupActivity;

/**
 * @author kelvin.nguyen
 * 
 */
public class TraVeActivity extends AbstractActivity {

	private DonHangTraVeAdapter mAdapter;
	private List<DonHang> donhangs = new ArrayList<DonHang>();
	private LoadMoreListView list_donhang_trave;
	private ProgressDialog dialog;

	private NhanVien nhanvien;
	private DatabaseHandler db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_trave);
		// define resource xml
		initView();
		// define database
		db = new DatabaseHandler(self);

		if (db.getAllNhanviens().size() > 0) {
			nhanvien = db.getAllNhanviens().get(0);
			// define resource xml
			initView();
			if (nhanvien != null) {
				donhangs = db.getAllDonHangsTraVe(DH_STATUS_RESTORE);
				if (donhangs.size() < 1) {
					new getDonHang().execute();
				} else {
					mAdapter = new DonHangTraVeAdapter(self, donhangs);
					list_donhang_trave.setAdapter(mAdapter);
				}
			}
		}
	}

	public void initView() {
		list_donhang_trave = (LoadMoreListView) findViewById(R.id.list_donhang_trave);
		list_donhang_trave.setOnItemClickListener(onItemClick);
	}

	public class getDonHang extends AsyncTask<Void, Void, List<DonHang>> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(getParent());
			dialog.setMessage(getResources().getString(R.string.label_dialog));
			dialog.show();
		}

		@Override
		protected List<DonHang> doInBackground(Void... params) {

			return db.getAllDonHangsTraVe(DH_STATUS_RESTORE);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(List<DonHang> result) {
			super.onPostExecute(result);
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			if (result.size() > 0) {
				mAdapter = new DonHangTraVeAdapter(self, donhangs);
				list_donhang_trave.setAdapter(mAdapter);
			}
		}

	}

	OnItemClickListener onItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long arg3) {
			view.setSelected(true);

			DonHang dh = (DonHang) parent.getAdapter().getItem(position);

			Intent intent_donhang = new Intent(self,
					DonHangTraVeDetailActivity.class);
			Bundle bundle=new Bundle();
			bundle.putParcelable(DONHANG, dh);
			bundle.putInt(STATUS, dh.getStatus());
			intent_donhang.putExtras(bundle);
			TabGroupActivity tabGroupActivity = (TabGroupActivity) getParent();
			tabGroupActivity.startChildActivity("DonHangTraVeDetailActivity",
					intent_donhang);

		}
	};
}
